package util.tree;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * R 结果
 * N node
 * L leaf
 * @date 2021/6/29
 */
public class TreeBuilderHelper {


    /**
     * 树
     */
    public interface Tree<R, N, L> {

        /**
         * 组装主干
         *
         * @param nodes
         * @return
         */
        Node<R, N, L> gatherNode(Collection<N> nodes, Function<N, R> nodeConvertFuc);


    }

    /**
     * 树with枝干
     */
    public interface Node<R, N, L> extends Tree<R, N, L> {

        /**
         * 组装枝叶
         *
         * @param leaf
         * @return
         */
        Leaf<R, N, L> gatherLeaf(Collection<L> leaf, Function<L, R> leafConvertFuc);

        /**
         * 过滤
         *
         * @param predicate
         * @return
         */
        Node<R, N, L> filter(Predicate<R> predicate);

        When<R, N, L> when(Supplier<Boolean> when);

        /**
         * 转化为树形态
         *
         * @return
         */
        List<R> build();

        List<R> buildWithRoot(Consumer<R> rootConsumer);

        List<R> buildWithNode(Consumer<R> nodeConsumer);

        List<R> buildWithLeaf(Consumer<R> leafConsumer);

    }

    /**
     * 树with叶
     */
    public interface Leaf<R, N, L> extends Node<R, N, L> {

    }

    /**
     * 条件
     *
     * @param <R>
     * @param <N>
     * @param <L>
     */
    public interface When<R, N, L> {

        Node<R, N, L> thenFilter(Predicate<R> predicate);

    }
}
